package com.icecloud.cloud.upms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icecloud.cloud.upms.dao.CoreUserDao;
import com.icecloud.cloud.upms.entity.CoreUser;
import com.icecloud.cloud.upms.entity.base.BaseCoreUser;

@Service
@Transactional(readOnly=true)
public class CoreUserService {
	
	@Autowired
	private CoreUserDao  coreUserDao;
	
	@Autowired
	private StandardPasswordEncoder standardPasswordEncoder;
	
	/**
	 * 根据名字查询用户
	 * @param username
	 * @return
	 */
	public BaseCoreUser findbyName(String username){
		return coreUserDao.findByName(username);
	}
	
	/**
	 * 保存用户
	 * @param coreUser
	 */
	@Transactional(readOnly=false)
	public void save(CoreUser coreUser){
		// 初始化实体参数
		coreUser.init();
		coreUser.setPassword(standardPasswordEncoder.encode(coreUser.getPassword()));
		coreUserDao.insert(coreUser);
	}

}
